package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.bean.BusRealTimeInfo;
import com.qut.graduate.busrtquery.bean.BusStation;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;
import com.qut.graduate.busrtquery.utils.common.LogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BusRealTimeInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "BusRealTimeInfoAdapter";
    private Context mContext;
    private List<BusRealTimeInfo> mBusRealTimeInfoList = new ArrayList<>();
    private List<BusStation> mBusStationList = new ArrayList<>();
    private List<BusStation> mBusStationListCopy = new ArrayList<>();
    private List<BusRealTimeInfo> mBusRealTimeInfoListCopy = new ArrayList<>();
    private int mBusRealTimeInfoDownSize;
    private int mDirect = BRTQConstant.BUS_DIRECT_UP; // 公交线路方向，1为上行，0为下行

    public BusRealTimeInfoAdapter(Context context, List<BusStation> busStationList, List<BusRealTimeInfo> busRealTimeInfoTotalList) {
        this.mContext = context;
        mBusRealTimeInfoList.clear();
        mBusRealTimeInfoList.addAll(busRealTimeInfoTotalList);
        mBusStationList.clear();
        mBusStationList.addAll(busStationList);
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  mBusStationList:" + mBusStationList);
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  busRealTimeInfoTotalList:" + busRealTimeInfoTotalList);
        for (int i = 0; i < mBusStationList.size(); i++) {
            if (mBusStationList.get(i) != null)
                if (mBusStationList.get(i).getDirection().equals(String.valueOf(BRTQConstant.BUS_DIRECT_UP)))
                    mBusStationListCopy.add(mBusStationList.get(i));
        }
        for (int i = 0; i < mBusRealTimeInfoList.size(); i++) {
            if (mBusRealTimeInfoList.get(i) != null)
                if (Integer.parseInt(mBusRealTimeInfoList.get(i).getStationseq()) < (mBusStationListCopy.size() + 1))
                    mBusRealTimeInfoListCopy.add(mBusRealTimeInfoList.get(i));
        }
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  busRealTimeInfoTotalList.size:" + busRealTimeInfoTotalList.size()
//                + "    mBusRealTimeInfoListCopy.size" + mBusRealTimeInfoListCopy.size()  + "    mBusStationListCopy.size" + mBusStationListCopy.size());
        mBusRealTimeInfoDownSize = mBusStationList.size() - mBusStationListCopy.size();
    }

    public void updateData(List<BusRealTimeInfo> busRealTimeInfoTotalList) {
        mBusRealTimeInfoList.clear();
        mBusRealTimeInfoListCopy.clear();
        mBusRealTimeInfoList.addAll(busRealTimeInfoTotalList);
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  updateData:" + busRealTimeInfoTotalList);
        for (int i = 0; i < mBusRealTimeInfoList.size(); i++) {
            if (mBusRealTimeInfoList.get(i) != null) {
                if (mDirect == BRTQConstant.BUS_DIRECT_UP && Integer.parseInt(mBusRealTimeInfoList.get(i).getStationseq()) < (mBusStationListCopy.size() + 1)) {
                    mBusRealTimeInfoListCopy.add(mBusRealTimeInfoList.get(i));
                } else if (mDirect == BRTQConstant.BUS_DIRECT_DOWN && Integer.parseInt(mBusRealTimeInfoList.get(i).getStationseq()) > mBusStationListCopy.size()) {
                    mBusRealTimeInfoListCopy.add(mBusRealTimeInfoList.get(i));
                }
            }
        }
        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  updateData:" + mBusRealTimeInfoListCopy);
        notifyDataSetChanged();
    }

    public void setmDirect(int mDirect) {
        this.mDirect = mDirect;
        mBusStationListCopy.clear();
        mBusRealTimeInfoListCopy.clear();
        for (int i = 0; i < mBusStationList.size(); i++) {
            if (mBusStationList.get(i) != null){
                if (mDirect == BRTQConstant.BUS_DIRECT_UP && mBusStationList.get(i).getDirection().equals(String.valueOf(BRTQConstant.BUS_DIRECT_UP))) {
                    mBusStationListCopy.add(mBusStationList.get(i));
                } else if (mDirect == BRTQConstant.BUS_DIRECT_DOWN && mBusStationList.get(i).getDirection().equals(String.valueOf(BRTQConstant.BUS_DIRECT_DOWN))) {
                    mBusStationListCopy.add(mBusStationList.get(i));
                }
            }
        }
        for (int i = 0; i < mBusRealTimeInfoList.size(); i++) {
            if (mBusRealTimeInfoList.get(i)!=null){
                if (mDirect == BRTQConstant.BUS_DIRECT_UP && Integer.parseInt(mBusRealTimeInfoList.get(i).getStationseq()) < (mBusStationListCopy.size() + 1)) {
                    mBusRealTimeInfoListCopy.add(mBusRealTimeInfoList.get(i));
                } else if (mDirect == BRTQConstant.BUS_DIRECT_DOWN && Integer.parseInt(mBusRealTimeInfoList.get(i).getStationseq()) > mBusStationListCopy.size()) {
                    mBusRealTimeInfoListCopy.add(mBusRealTimeInfoList.get(i));
                }
            }
        }
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  mBusStationListCopy:" + mBusStationListCopy);
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  mBusRealTimeInfoListCopy:" + mBusRealTimeInfoListCopy);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mBusStationListCopy != null ? mBusStationListCopy.size() : 0;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bus_real_time_info, parent, false);
        return new BusRealTimeInfoHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        BusRealTimeInfoHolder busRealTimeInfoHolder = (BusRealTimeInfoHolder) holder;
        // 强制关闭viewholder复用
        busRealTimeInfoHolder.setIsRecyclable(false);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < mBusRealTimeInfoListCopy.size(); i++) {
            if (mBusStationListCopy.get(position).getStaionname().equals(mBusRealTimeInfoListCopy.get(i).getCurrent_station_name())) {
                stringBuilder.append(mBusRealTimeInfoListCopy.get(i).getCar_num()).append("\t\t").append(mBusRealTimeInfoListCopy.get(i).getTime_to_there()).append("\n");
            }
        }
        String busCarInfo = stringBuilder.toString();
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  busCarInfo:" + busCarInfo);
//        LogUtils.d("----实时公交信息界面 BusRealTimeInfoAdapter  busCarInfo 换行符的index:" + busCarInfo.indexOf("\n") +
//                "    busCarInfo.length():" + busCarInfo.length());
        if (busCarInfo.contains("\n") && busCarInfo.indexOf("\n") + 1 == busCarInfo.length()) {
            busCarInfo = busCarInfo.substring(0, busCarInfo.indexOf("\n"));
        }
        busRealTimeInfoHolder.mBusRealTimeInfo.setText("");
        if (stringBuilder.toString().equals("")) {
            busRealTimeInfoHolder.itemView.setVisibility(View.INVISIBLE);
//            busRealTimeInfoHolder.mBusRealTimeInfoLayout.setVisibility(View.INVISIBLE);
        } else {
            busRealTimeInfoHolder.itemView.setVisibility(View.VISIBLE);
//            busRealTimeInfoHolder.mBusRealTimeInfoLayout.setVisibility(View.VISIBLE);
            busRealTimeInfoHolder.mBusRealTimeInfo.setText(busCarInfo);
        }
    }

    public class BusRealTimeInfoHolder extends RecyclerView.ViewHolder {
        TextView mBusRealTimeInfo;
        LinearLayout mBusRealTimeInfoLayout;

        public BusRealTimeInfoHolder(View view) {
            super(view);
            mBusRealTimeInfo = view.findViewById(R.id.bus_real_time_info);
            mBusRealTimeInfoLayout = view.findViewById(R.id.bus_real_time_info_layout);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

}
